package RMService;

import java.util.List;
import java.util.Map;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransportException;
import org.ini4j.Ini;

import Node.TMultiplexedProtocol;
import Node.TransportLayer;

public class RMClient {
	
	// Member variables
	private TSocket transport;
	private RMService.Client RMProto;
	
	/**
	 * Constructor for RMClient object.
	 * 
	 * @param serviceConfig
	 * @param server
	 * @param serverPort
	 * @throws TTransportException
	 */
	public RMClient(String server, int serverPort) throws TTransportException {
	
		/*
		 * Setup transport layer
		 */
		transport = new TSocket(server, serverPort);
		
		/*
		 * Initialize protocol: JSON, compact, binary, etc.
		 */
		TBinaryProtocol protocol = new TBinaryProtocol(transport);

		/*
		 * Register service (service name must match service name registered on server)
		 */
		TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "RMService");
		
		RMProto = new RMService.Client(mp);
	}
	
	public void deleteReplicas(int key, int ttl, int origin) throws TException{
		transport.open();
		RMProto.deleteReplicas(key, ttl, origin);
		transport.close();
	}
	
	public void replicate(int key, String value, int ttl, int origin) throws TException{
		transport.open();
		RMProto.replicate(key, value, ttl, origin);
		transport.close();
	}
	
	
	public void getKeys(List<Integer> buckets, Map<Integer, String> keyValues, int origin, int ttl) throws TException{
		transport.open();
		RMProto.getKeys(buckets, keyValues, origin, ttl);
		transport.close();
	}
	
}

